//
//  ST_PairsCenterFooterView.swift
//  btvideo
//
//  Created by mac on 2020/3/21.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SnapKit
import SwifterSwift
import SwiftyJSON
//import ST_CHTCollectionViewWaterfallLayout

/**
 个人主页推荐
 */
class ST_PairsCenterFooterView: UIView {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
    
        self.backgroundColor = UIColor.init(hex: 0xACB4C0)
        
        self.addSubview(root_view)
        root_view.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.init(top: 2, left: 0, bottom: 0, right: 0))
        }
        
        
        root_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(25)
        }
        
        root_view.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.top.equalToSuperview().offset(61)
        }
        
        NotificationCenter.default.addObserver(self, selector: #selector(receive_liked_somebody_handle(notification:)), name: Notification.Name.liked_somebody, object: nil)
        
    }
    

    @objc fileprivate func receive_liked_somebody_handle(notification:Notification){
        
        
        let json = JSON(notification.userInfo ?? "")
        
        let removeItem = self.data_array.removeFirst { (item_model) -> Bool in
            return item_model.accid == json["id"].stringValue
        }
        
        if removeItem != nil {
            
            self.collectionView.reloadData()
            
        }
    }
    
    
    override func willMove(toSuperview newSuperview: UIView?) {
        super.willMove(toSuperview: newSuperview)
        
        if newSuperview == nil{
            
        }
        
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
//    MARK:-properties
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = "相似的人"
        control.textColor = Color.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var root_view:UIView = {
       let control = UIView()
        control.cornerRadius = 30
        control.backgroundColor = .white
        return control
    }()
    
    
    fileprivate lazy var layout:ST_CHTCollectionViewWaterfallLayout = {
       let controlLayout = ST_CHTCollectionViewWaterfallLayout()
        controlLayout.minimumColumnSpacing = ST_PinterestHomeCellMinimumColumnSpacing
        controlLayout.minimumInteritemSpacing = 10
        controlLayout.sectionInset = UIEdgeInsets.init(top: 0, left: 10, bottom: 30, right: 10)
        return controlLayout
    }()
    
    fileprivate lazy var collectionView:UICollectionView = {
        let control = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: self.layout)
        control.backgroundColor = Color.white
        control.delegate = self
        control.dataSource = self
        control.register(cellWithClass: ST_PinterestHomeCell.self)
        control.register(supplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withClass: ST_PeculiarityDetialSupplementView.self)
        if #available(iOS 11.0, *) {
            control.contentInsetAdjustmentBehavior = .never
        }
        control.isScrollEnabled = false
        control.showsVerticalScrollIndicator = false
        control.showsHorizontalScrollIndicator = false
        return control
    }()
    
    var modify_height_closure:((CGFloat)->())?
    
    var data_array = [ST_PinterestHomeCellModel](){
        didSet{
            
            self.collectionView.reloadData {
            
                self.collectionView.layoutIfNeeded()
                self.layout.invalidateLayout()
                
                let figuredSize = self.layout.collectionViewContentSize
                
                print("bug 2542---Size:",figuredSize,"---data_array count:",self.data_array.count)
                
                self.modify_height_closure?(figuredSize.height + 62)
            }
            
        }
    }
}


//MARK: - UICollectionView Delegate
extension ST_PairsCenterFooterView:CHTCollectionViewDelegateWaterfallLayout,UICollectionViewDataSource{
    
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return data_array[safe:indexPath.item]?.size ?? CGSize.zero
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data_array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withClass: ST_PinterestHomeCell.self, for: indexPath)
        
        if let m = data_array[safe:indexPath.item]{
            cell.model = m
        }
        
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
     
        let vc = ST_PairsCenterViewController()
        
        vc.accid = data_array[indexPath.item].accid
        
        let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
        
        nav.overCurrentContext = true
        
        GlobalPresent(toController: nav)
        
    }
    
}
